﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LevelEditor.Objects;
using System.Windows.Forms;
using System.Drawing;

namespace LevelEditor.Objects.Tiles
{
    class Door : Tile
    {
        public enum Variables
        {
            PositionX, PositionY, NameRoom, IsOpen, IsClosedForever, IsBlocked, LoadNextPartLevel,
            StandAlonebehavior
        }

        #region Variables
        private string nameRoom = "";
        public string NameRoom
        {
            get { return nameRoom; }
            set { nameRoom = value; }
        }

        private string loadNextPartLevel = "";
        public string LoadNextPartLevel
        {
            get { return loadNextPartLevel; }
            set { loadNextPartLevel = value; }
        }

        private bool isOpen = true;
        public bool IsOpen
        {
            get { return isOpen; }
            set { isOpen = value; }
        }

        private bool isClosedForever;
        public bool IsClosedForever
        {
            get { return isClosedForever; }
            set { isClosedForever = value; }
        }

        private bool isBlocked;
        public bool IsBlocked
        {
            get { return isBlocked; }
            set { isBlocked = value; }
        }
        #endregion

        #region Events

        public override void Deselect()
        {
            base.Deselect();
            editor.propertiesPanel1.HideProperties(this);
        }

        protected override void Tile_MouseMove(object sender, MouseEventArgs e)
        {
            base.Tile_MouseMove(sender, e);

        }

        protected override void Tile_MouseUp(object sender, MouseEventArgs e)
        {
            base.Tile_MouseUp(sender, e);

            if (Tile.CurrentSelectedTile != null)
                editor.propertiesPanel1.Refresh();
        }

        public void PositionXChanged(object sender, EventArgs args)
        {
            if (((TextBox)sender).Text == "") return;

            Point p = Point.Empty;

            try{ p = new Point(int.Parse(((TextBox)sender).Text), this.Location.Y); }
            catch (FormatException) { }

            this.Location = p;

            this.Father.Tile.Location = p;
        }

        public void PositionYChanged(object sender, EventArgs args)
        {
            if (((TextBox)sender).Text == "") return;

            Point p = Point.Empty;

            try { p = new Point(this.Location.X, int.Parse(((TextBox)sender).Text)); }
            catch (FormatException) { }
            
            this.Location = p;

            this.Father.Tile.Location = p;
        }

        public void NameRoomChanged(object sender, EventArgs args)
        {
            if (((TextBox)sender).Text == "") return;

            NameRoom = (sender as TextBox).Text;
        }

        public void LoadNextPartLevelClicked(object sender, EventArgs args)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            DialogResult r = dialog.ShowDialog();
            if (r == DialogResult.OK)
            {
                //LoadNextPartLevel = dialog.FileName;
                ((TextBox)sender).Text = dialog.SafeFileName;
                LoadNextPartLevel = (sender as TextBox).Text;
            }
            else if (r == DialogResult.Cancel)
            {
                if (string.IsNullOrEmpty(dialog.SafeFileName)){
                    ((TextBox)sender).Text = "";
                    LoadNextPartLevel = (sender as TextBox).Text;
                }
            }
        }

        public void IsOpenClicked(object sender, EventArgs args)
        {
            IsOpen = (sender as CheckBox).Checked;
        }

        public void IsClosedForeverClicked(object sender, EventArgs args)
        {
            IsClosedForever = (sender as CheckBox).Checked;
        }

        public void IsBlockedClicked(object sender, EventArgs args)
        {
            IsBlocked = (sender as CheckBox).Checked;
        }


        #endregion



        public Door(LevelEditor editor)
            : base(editor)
        {
            this.myType = TileType.Door;
        }
    }
}
